<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="宴会厅名称" prop="banquet_name">
				<el-input v-model="form.banquet_name" placeholder="请输入宴会厅名称" clearable></el-input>
			</el-form-item>
            <el-form-item label="酒店地址" prop="banquet_address">
				<el-input v-model="form.banquet_address" placeholder="请输入酒店地址" clearable></el-input>
			</el-form-item>
            <el-form-item label="所属酒店" prop="hotel_id">
                <el-select v-model="form.hotel_id" placeholder="请选择所属酒店" style="width:100%" clearable filterable>
                    <el-option v-for="item in hotelList" :key="item.id" :label="item.hotel_name" :value="item.id"/>
                </el-select>
            </el-form-item>
			<!-- <el-form-item label="创建时间" prop="date">
                <el-date-picker v-model="form.date" type="date" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
            </el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					banquet_name: "",
                    banquet_address:"",
					// date:this.moment().format('YYYY-MM-DD'),
                    hotel_id: "",
				},
				//验证规则
				rules: {
					banquet_name: [
						{required: true, message: '请输入宴会厅名称', trigger: 'change'}
					],
				},
                hotelList:[],
			}
		},
		mounted() {
            this.getHotel()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.product.banquet.edit.post(this.form);
						}else{
							res = await this.$API.product.banquet.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.banquet_name = data.banquet_name
				this.form.banquet_address = data.banquet_address
				this.form.hotel_id = data.hotel_id
				// this.form.date = data.date
			},
            async getHotel(){
                var res = await this.$API.client.hotel.list.get();
                this.hotelList = res.data.rows;
            },
		}
	}
</script>

<style>
</style>
